package cn.wzut.edut.controller;

import cn.wzut.edut.jwt.PassToken;
import cn.wzut.edut.pojo.JsonResult;
import cn.wzut.edut.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

/**
 * 身份验证（登录）控制层
 * by 叶庭辉
 */
@Validated
@RestController
public class LoginController {

    @Autowired
    LoginService loginService;

    /**
     * 登录请求
     * @return json数据报（data内为返回数据）
     */
    @PassToken
    @PostMapping("/login")
    public JsonResult Login(@RequestParam(value = "uid") @Pattern(regexp = "\\d{11}", message = "用户名错误") @NotNull(message = "用户名不能为空") String uid,
                            @RequestParam(value = "pwd") @Pattern(regexp = "[a-zA-Z0-9-*/+.~!@#$%^&*()]{6,20}$", message = "密码错误") @NotNull(message = "密码不能为空") String pwd){
        return new JsonResult(loginService.Login(uid, pwd));
    }

}
